package com.xlh.server.protocol.support.json;

import android.view.View;

import com.xlh.Utils.GsonManager;
import com.xlh.Utils.MudProtocol;
import com.xlh.bean.ProtocolObject.Base;
import com.xlh.bean.ProtocolObject.InputBean;
import com.xlh.interf.IActivity;
import com.xlh.interf.ITakeMessage;

/**
 * 输入框
 * Created by mike@Q184377367 on 2018/7/2.
 */

public class InputBoxServer implements ITakeMessage {

    private IActivity atv;
    private String hasType = "input";

    public InputBean getProtccoObj() {
        return protccoObj;
    }

    public InputBoxServer setProtccoObj(InputBean protccoObj) {
            this.protccoObj = protccoObj;
        return this;
    }

    private InputBean protccoObj;
    public InputBoxServer(IActivity atv){
        this.atv = atv;
    }

    @Override
    public void takeMessage(String t,String s){

        final InputBoxServer me = this;

        if(!t.equals(this.hasType)) return;

        protccoObj =  GsonManager.gsmgr.jsonToAnyObject(s, InputBean.class);

        if(protccoObj==null ) return;
        this.renderView();

        }

    @Override
    public boolean takeMessage(String s) {
        return false;
    }

    @Override
    public void init() {

    }

    @Override
    public void atViewLoadAfterInit(){



    }


    public void renderView(){

        final InputBoxServer me = this;

        // 001当前频道【闲聊】，请输入内容：$zj#chat $txt#
        me.atv.getActivity().lvFloatRightList.setVisibility(View.GONE);
        me.atv.getActivity().getmAlertList().getDataAdapter().clear();
        me.atv.getActivity().getmAlertGrid().getDataAdapter().clear();
        //找出输入框的大段文字描述
        me.atv.getActivity().getmAlertInputBigText().setText(protccoObj.getInput().getDesc());






        if (me.atv.getActivity().getEtInput() != null) {
            me.atv.getActivity().getEtInput().setHint(protccoObj.getInput().getHitText());
            me.atv.getActivity().getEtInput().setText("");
        }

        if (me.atv.getActivity().getmAlertInputBigText().getVisibility() == View.GONE) {
            me.atv.getActivity().getmAlertInputBigText().setVisibility(View.VISIBLE);
        }

        if (me.atv.getActivity().mAlertLayout.getVisibility() == View.INVISIBLE) {
            me.atv.getActivity().mAlertLayout.setVisibility(View.VISIBLE);
        }



        me.atv.getActivity().getInputBoxBut().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (me.atv.getActivity().mSocketClient != null) {

                    if (me.atv.getActivity().getEtInput() != null) {

                        String action = me.atv.getActivity().getEtInput().getText().toString();
                        String msg="";
                        //有CMD通常CMD都是服务端传来需要替换的
                        if( protccoObj.getInput().getCmd().isEmpty()){
                             msg = me.atv.getActivity().getEtInput().getText().toString();
                        }else{
                             msg = protccoObj.getInput().getCmd().replace(MudProtocol.WZREPSTR, action);
                        }


                        //Log.e("result", "" + msg);
                        //隐藏大段文字框
                        //mAlertInputBigText.setVisibility(View.GONE);
                        //mAlertInputBigText.setText("");
                        me.atv.getActivity().mSocketClient.sendMsg(msg);
                        me.atv.getActivity().mAlertLayout.setVisibility(View.INVISIBLE);
                        me.atv.getActivity().lvFloatRightList.setVisibility(View.VISIBLE);

                    }
                }
            }
        });

        //隐藏其他类型对话框的描述文字
        if (me.atv.getActivity().getmAlertText().getVisibility() == View.VISIBLE) {
            me.atv.getActivity().getmAlertText().setVisibility(View.GONE);
        }
        if (me.atv.getActivity().getmInputLayout().getVisibility() == View.INVISIBLE) {
            me.atv.getActivity().getmInputLayout().setVisibility(View.VISIBLE);
        }




    }





}
